package com.hyt.it.ogt.ykcj.mq;

import com.hyt.it.ogt.ykcj.domain.exchange.MessageInfo;
import com.hyt.it.ogt.ykcj.service.exchange.IApiLogService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 同步考试数据到成绩队列消息消费者
 *
 * @author yanghuankun@talkweb.com.cn
 * @date 2023-4-10
 */
@Component
@Slf4j
public class SyncCandidateToCjConsumer {

    @Resource
    SyncCandidateToCjProducer syncCandidateToCjProducer;
    @Resource
    IApiLogService iApiLogService;

    @RabbitListener(bindings = { 
                        @QueueBinding(value = @Queue(value = "${mq.syncCandidateToCj.queue}", 
                                                     autoDelete = "false", 
                                                     durable = "true"), 
                                                     exchange = @Exchange(value = "${mq.kzToKhDirect}", 
                                                     type = ExchangeTypes.DIRECT), 
                                                     key = {"${mq.syncCandidateToCj.route}" }
                                      ) 
                        }, concurrency = "${mq.syncCandidateToCj.concurrency}")
    public void syncCandidateToCjListener(String msg, Message message, Channel channel) throws IOException {
        log.info("## 同步考试数据到成绩队列消息消费：{}", msg);
        try {
            MessageInfo messageInfo = new MessageInfo();
            messageInfo.setMsg(msg);
            iApiLogService.saveCandidateInfo(messageInfo);
        } catch (Exception e) {
            log.error("同步考试数据到成绩队列消息消费异常：{}，进行重新投递", msg, e);
            //syncCandidateToCjProducer.sendMessage(msg);
        } finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}
